package org.example.buysystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.xml.fastinfoset.stax.events.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.buysystem.common.Result;
import org.example.buysystem.constants.Constants;
import org.example.buysystem.entity.LoginForm;
import org.example.buysystem.entity.User;
import org.example.buysystem.entity.dto.UserDto;
import org.example.buysystem.service.UserService;
import org.example.buysystem.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@Api(value = "登录",tags = {"登录模块"})
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    @ApiOperation("登录模块")
    public Result login(@RequestBody LoginForm loginForm){
       UserDto userDto= userService.login(loginForm);
       return Result.success(userDto);

    }

    @PostMapping("/register")
    @ApiOperation("注册模块")
    public Result register(@RequestBody LoginForm loginForm){
        User user=userService.register(loginForm);
        return Result.success();
    }

    @GetMapping("/userinfo/{username}")
    @ApiOperation("获取用户信息")
    public Result getUserInfo(@PathVariable String username){
        User user=userService.getUsername(username);
        return Result.success(user);
    }

    @GetMapping("/userid")
    @ApiOperation("获取用户信息")
    public long getUserId(){
        return TokenUtils.getCurrentUser().getId();
    }


    @GetMapping("/user/resetPassword")
    @ApiOperation("重置密码")
    public Result resetPassword(@RequestParam String id,@RequestParam String newPassword) {
        userService.resetPassword(id, newPassword);
        return Result.success();
    }

    @GetMapping("/user/page")
    @ApiOperation("分页查询用户")
    public Result getUserPage(
            @RequestParam(required = false,defaultValue = "1") int pageNum,
            @RequestParam(required = false,defaultValue = "10") int pageSize,
            String id,
            String username,
            String nickname
    ) {

        IPage<User> userIPage=new Page<>(pageNum,pageSize);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(!Util.isEmptyString(id)){
            queryWrapper.like("id",id);
        }
        if(!Util.isEmptyString(username)){
            queryWrapper.like("username",username);
        }
        if(!Util.isEmptyString(nickname)){
            queryWrapper.like("nickname",nickname);
        }
        queryWrapper.orderByDesc("id");
        System.out.println("============"+TokenUtils.getCurrentUser());
        return Result.success(userService.page(userIPage,queryWrapper));

        }


        @PostMapping("/user")
        @ApiOperation("保存用户")
        public Result saveUser(@RequestBody User user){
            return userService.saveUpdate(user);
        }


        @DeleteMapping("/user/{id}")
        @ApiOperation("根据id删除用户")
        public Result deleteUser(@PathVariable String id){
            boolean isSuccessful=userService.removeById(id);

            if(isSuccessful){
                return Result.success();
            }else{
                return Result.error(Constants.CODE_500,"删除失败");
            }
        }

        @PostMapping("/user/del/batch")
        @ApiOperation("批量删除用户")
        public Result deleteBatchUser(@RequestBody List<Integer> ids){
            boolean isSuccessful=userService.removeBatchByIds(ids);
            if(isSuccessful){
                return Result.success();
            }else
                return Result.error(Constants.CODE_500,"批量删除失败");
        }


}
